package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("/business/bpmnInfo")
public class BpmnInfoController {
    //前缀
    private static final String prefix ="/business/bpmnInfo/";
    @Autowired
    private IBpmnInfoService bpmnInfoService;
    /***************************   进入页面开始    *****************************/
    //列表
    @RequestMapping("/listPage")
    public String list(){
        return prefix + "list";
    }
    //流程文件部署 页面
    @RequestMapping("/deployPage")
    public String deployPage(){
        return prefix + "deploy";
    }
    /***************************   进入页面结束    *****************************/

    //分页查询 功能
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo){
       return bpmnInfoService.query(qo);
    }

    //流程文件部署 功能
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult deploy(MultipartFile file,String bpmnType,String bpmnLabel,String info) throws IOException {
        bpmnInfoService.deploy(file,bpmnLabel,bpmnType,info);
        return AjaxResult.success();
    }
    // 查看流程文件 流程图
    @RequestMapping("/readResource")
    @ResponseBody
    public void readResource(HttpServletResponse response,String type,Long id) throws IOException {
        //根据类型去找对应的一个流
      InputStream is = bpmnInfoService.getInputStreamByType(id,type);
        IOUtils.copy(is,response.getOutputStream());
    }
    //删除流程定义
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        bpmnInfoService.delete(id);
        return AjaxResult.success();
    }
}
